import {getCurrentWebviewWindow, WebviewWindow} from "@tauri-apps/api/webviewWindow";

export interface WindowsProps {
    label: string;
    url?: string;
    title: string;
    minWidth: number;
    minHeight: number;
    width: number;
    height: number;
    closeWindLabel?: string;
    resizable: boolean;
}

class WindowHandle {
    async createWindows(
        args: WindowsProps = {
            label: "main",
            title: "主窗口",
            minWidth: 800,
            minHeight: 600,
            width: 800,
            height: 600,
            resizable: true,
        }
    ) {
        if (!await this.isExist(args.label)) {
            const webview = new WebviewWindow(args.label, {
                title: args.title,
                url: args.url,
                fullscreen: false,
                resizable: args.resizable,
                center: true,
                width: args.width,
                height: args.height,
                minWidth: args.minWidth,
                minHeight: args.minHeight,
                skipTaskbar: false,
                decorations: false,
                transparent: false,
                titleBarStyle: "overlay",
                hiddenTitle: true,
                visible: false, // 创建隐层掉
            });
            webview.once("tauri://created", () => {
                if (args.closeWindLabel) {
                    WebviewWindow.getByLabel(args.closeWindLabel)
                        .then(win => win?.close())
                        .catch(error => console.error("关闭窗口失败:", error));
                }
            }).then();

            // 修复点2：使用 .then() 链式调用
            webview.once("tauri://error", () => {
                if (args.closeWindLabel) {
                    this.showWindow(args.closeWindLabel)
                        .catch(error => console.error("显示窗口失败:", error));
                }
            }).then();
            return webview;
        } else {
            await this.showWindow(args.label);
        }
    }

    nowWindow () {
        return getCurrentWebviewWindow();
    }
    async getWindow(label?: string) {
        return label ? await WebviewWindow.getByLabel(label) : getCurrentWebviewWindow();
    }
    async minimizeWindow(label?: string) {
        const win = await this.getWindow(label);
        await win?.minimize();
    }
    async toggleMaximize(label?: string) {
        const win = await this.getWindow(label);
        await win?.toggleMaximize();
    }
    async closeWindow(label?: string) {
        const win = await this.getWindow(label);
        await win?.close();
    }
    async showWindow(label: string, isCreated: boolean = false) {
        const window = await WebviewWindow.getByLabel(label);

        if (!window) {
            if (!isCreated) {
                return this.createWindows();
            }
            return;
        }

        // 一次性执行所有检查
        const [isVisible, isMinimized] = await Promise.all([
            window.isVisible(),
            window.isMinimized()
        ]);

        if (!isVisible) await window.show();
        if (isMinimized) await window.unminimize();
        await window.setFocus();
    }
    async isExist(label: string) {
        const isExistsWinds = await WebviewWindow.getByLabel(label);
        return !!isExistsWinds;
    }
}
export default new WindowHandle();
